import time
from selenium import webdriver
from selenium.webdriver.common.by import By

# clear()
# get_attribute()获取属性值
# is_displayed()判断元素是否可见
# is_enabled()判断元素是否可用

class TestCase():
    def __init__(self):
        self.driver = webdriver.Chrome()

    def clear(self):
        self.driver.get('https://www.baidu.com')
        time.sleep(2)
        kw = self.driver.find_element(By.ID, 'kw')
        kw.send_keys('天气预报')

        time.sleep(2)
        kw.clear()

        time.sleep(2)
        kw.send_keys('热干面')

    def get_attribute(self):
        # 获取标签html属性
        self.driver.get('https://www.baidu.com')
        kw = self.driver.find_element(By.ID, 'kw')
        print(kw.get_attribute('outerHTML'))
        print(kw.get_attribute('id'))
        print(kw.get_attribute('class'))
        print(kw.get_attribute('name'))
        kw.get_attribute('autocomplete')

    def is_display(self):
        # 判断标签是否显示在页面中,显示就是True,不显示False
        self.driver.get('https://www.baidu.com')
        ie = self.driver.find_element(By.XPATH, '//*[@id="form"]/input[1]')
        print(ie.is_displayed())
        kw = self.driver.find_element(By.ID, 'kw')
        print(kw.is_displayed())

    def is_enabled(self):
        # is_enabled() 判断元素是否可用, 可用返回True,不可用返回False
        self.driver.get('https://sahitest.com/demo/clicks.htm')
        btn1 = self.driver.find_element(By.CSS_SELECTOR, 'body > form > input[type=button]:nth-child(16)')
        print(btn1.is_enabled())

        btn2 = self.driver.find_element(By.XPATH, '/html/body/form/input[4]')
        print(btn2.is_enabled())

if __name__ == '__main__':
    case = TestCase()
    # case.clear()
    # case.get_attribute()
    # case.is_display()
    case.is_enabled()

    time.sleep(2)
    case.driver.quit()
